package boun.swe.bob.entities;

import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Created by IntelliJ IDEA.
 * User: ememory
 * Date: 27.Ara.2010
 * Time: 15:22:01
 * To change this template use File | Settings | File Templates.
 */
/**
 * This entity corresponds to user_message table in db
 */
@Entity
@Table(name = "groups")

public class Group {

    @Id
    @GenericGenerator(name = "generator", strategy = "increment")
    @GeneratedValue(generator = "generator")
    @Column(name = "group_id")
    private Integer groupId;

    @Column(name = "group_name")
    private String groupName;

    @Column(name = "description")
    private String description;

    @Column(name = "picture_url")
    private String pictureUrl;

    @Column(name = "create_date")
    private Timestamp createDate;

    @ManyToOne(cascade=CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "owner_user_id")
    private User ownerUser;

    @ManyToMany(cascade=CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "groups")
    private Set<User> members;

    @OneToMany(cascade=CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "group_id")
    @OrderBy("feedId DESC")
    private Set<GroupFeed> groupFeeds;


    public Integer getGroupId() {
        return groupId;
    }

    public void setGroupId(Integer groupId) {
        this.groupId = groupId;
    }

    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getPictureUrl() {
        return pictureUrl;
    }

    public void setPictureUrl(String pictureUrl) {
        this.pictureUrl = pictureUrl;
    }

    public Timestamp getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Timestamp createDate) {
        this.createDate = createDate;
    }

    public Set<User> getMembers() {
        return members;
    }

    public void setMembers(Set<User> members) {
        this.members = members;
    }

    public User getOwnerUser() {
        return ownerUser;
    }

    public void setOwnerUser(User ownerUser) {
        this.ownerUser = ownerUser;
    }
    public List<User> getTopMembersList() {
        int limit = Math.min(members.size(), 10);
        List<User> list = new ArrayList<User>(members).subList(0, limit);
        return list;
    }

    public List<GroupFeed> getTopFeedsList(){
        int limit= Math.min(groupFeeds.size(),15);
        List<GroupFeed> list = new ArrayList<GroupFeed>(groupFeeds).subList(0, limit);
        return list;
    }

    public void addMember(User member)
    {
        this.members.add(member);
    }
}